// pages/position/index/index.js
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: '',
    index: '',
    tabs: [{
      key: 'tab1',
      title: '全职职位',
    },
    {
      key: 'tab2',
      title: '小时工',
    }
    ],
    pageSize: 10,
    posList:[],
    isLoadedPos:false,
    isFirstTipPos: true,
    pagePos: 1,

    hourlyList:[],
    isLoadedHourly:false,
    isFirstTipHourly: true,
    pageHourly: 1,


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    token = getApp().globalData.token;
    this.getFulltimeList();
    this.getHourlyList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  toast(txt) {
    wx.showToast({
      title: txt,
      icon: 'none'
    })
  },

  onTabsChange(e) {
    const {
      key
    } = e.detail;
    const index = this.data.tabs.map((n) => n.key).indexOf(key);

    this.setData({
      key,
      index,
    });
  },

  onSwiperChange(e) {
    const {
      current: index,
      source
    } = e.detail
    const {
      key
    } = this.data.tabs[index]

    if (!!source) {
      this.setData({
        key,
        index,
      })
    }
  },

  getFulltimeList() {
    console.log('全职职位', this.data.pagePos, this.data.pageSize);
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'position/list',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pagePos,
        size: this.data.pageSize
      },
      success: (res) => {
        if (res.rowCount == this.data.posList.length) {
          this.data.isLoadedPos = true;
          return
        }
        
        this.setData({
          posList: this.data.posList.concat(res.list)
        })
      }
    })
  },

  getHourlyList() {
    console.log('小时工', this.data.pageHourly, this.data.pageSize);
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'position_hourly/list',
      method: 'GET',
      data: {
        token: token,
        page: this.data.pageHourly,
        size: this.data.pageSize
      },
      success: (res) => {
        if (res.rowCount == this.data.hourlyList.length) {
          this.data.isLoadedHourly = true;
          return
        }
        this.setData({
          hourlyList: this.data.hourlyList.concat(res.list)
        })
      }
    })
  },

  onLoadMorePosition() {
    if (this.data.isLoadedPos) {
      if (this.data.isFirstTipPos) {
        this.toast('已加载完全部数据了');
      }
      this.data.isFirstTipPos = false;
      return;
    }
    this.data.pagePos += 1;
    this.getFulltimeList();
  },

  onLoadMoreHourly(){
    if (this.data.isLoadedHourly) {
      if (this.data.isFirstTipHourly) {
        this.toast('已加载完全部数据了')
      }
      this.data.isFirstTipHourly = false;
      return;
    }
    this.data.pageHourly += 1;
    this.getHourlyList();
  },

  onPositionDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../fullTime/detail/detail?id=' + id,
    })
  },

  onHourlyDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../hourly/detail/detail?id=' + id,
    })
  },

  onAddPosition() {
    if (this.data.index == 0) { //普通职位 
      wx.navigateTo({
        url: '../fullTime/add/add',
      })
    } else  { //小时工
      wx.navigateTo({
        url: '../hourly/add/add',
      })
    }
  }
})